package com.angel.web.sys;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.angel.core.util.JsonUtil;
import com.angel.core.util.Pager;
import com.angel.core.util.ResultParam;
import com.angel.core.util.StringUtils;
import com.angel.core.util.TreeNodes;
import com.angel.health.SysOffice;
import com.angel.health.SysOfficeExt;
import com.angel.provider.sys.SysManagerDepartProvider;

/**
 * 高亚林
 * 2016年9月20日下午1:07:00
 */

@RequestMapping("/manag/depart")
@Controller
public class SysManagerDepartController {

	@Autowired
	private SysManagerDepartProvider provider;
	
	//查询所有公司
	@RequestMapping(value = "findAllDepart", produces = "application/json; charset=utf-8")
	@ResponseBody
	public String findAllDepart(SysOffice office){
		List<SysOffice> list = provider.findAllDepart(office);
		List<TreeNodes> treeList = new ArrayList<TreeNodes>();
		for (SysOffice sysOffice : list) {
			TreeNodes node = new TreeNodes();
			node.setId(sysOffice.getId());
			node.setpId(sysOffice.getParentId());
			node.setName(sysOffice.getName());
			node.setOpen("true");
			treeList.add(node);
		}
		return JsonUtil.toJson(treeList);		
	}
	
	//分页查询
	@RequestMapping("/index")
	public String index(SysOffice office,Pager pagers,HttpServletRequest request){
		String url = request.getRequestURL().toString();
		String param = office.getParentIds();
		if(!StringUtils.isEmpty(param)){
			url += "?parentIds="+ param;
		}
		Pager pager = provider.findAll(office, pagers);
		request.setAttribute("pager", pager);
		request.setAttribute("url", url);
		System.out.println(url);
		return "manag/depart";
	}
	
	//批量删除
	@RequestMapping("delete")
	@ResponseBody
	public String delete(String ids){
		ResultParam param = provider.delete(ids);
		return JsonUtil.toJson(param);
	}
	
	//删除单个用户
	@RequestMapping("deleteById")
	@ResponseBody
	public String deleteById(SysOffice office){
		ResultParam param = provider.deleteById(office);
		return JsonUtil.toJson(param);
	}
	
	//保存
	@RequestMapping("/save")
	@ResponseBody
	public String save(SysOffice office,HttpServletRequest request){
		office.setCreateBy(request.getSession().getAttribute("user").toString());
		Map<String, String> map = provider.save(office);
		return JsonUtil.toJson(map);
	}
	
	//根据id获取数据
	@RequestMapping("/edit")
	@ResponseBody
	public String edit(SysOffice office){
		SysOfficeExt ext = provider.findUserById(office);
		return JsonUtil.toJson(ext);
	}
	
	//修改
	@RequestMapping("/update")
	@ResponseBody
	public String update(SysOffice office,HttpServletRequest request){
		office.setUpdateBy(request.getSession().getAttribute("user").toString());
		Map<String, String> map = provider.update(office);
		return JsonUtil.toJson(map);
	}
	
}
